import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import { viteMockServe } from 'vite-plugin-mock'
import http from "http"

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  // 获取各种环境下对应的变量
  let env = loadEnv(mode, process.cwd());

  console.log(env, '环境')
  return {
    plugins: [
      vue(),
      createSvgIconsPlugin({
        // Specify the icon folder to be cached
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
        // Specify symbolId format
        symbolId: 'icon-[dir]-[name]',
      }),
      viteMockServe({
        localEnabled: command === 'serve', //保证开发阶段能够使用mock接口
      }),
    ],
    resolve: {
      alias: {
        "@": path.resolve("./src") // 相对路径别名配置，使用 @ 代替 src
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          javascriptEnabled: false,
          additionalData: `@import "./src/styles/variable.scss";`,
        }
      }
    },
    // 代理跨域
    server: {
      host: '0.0.0.0',
      proxy: {
        [env.VITE_APP_BASE_API]: {
          // '/api': {
          // 获取数据的服务器地址设置
          // env.VITE_SERVE
          target: env.VITE_SERVE,
          // 需要代理跨域
          changeOrigin: true,
          secure: true,  // 如果是https接口，需要配置这个参数      
          agent: new http.Agent(),
          // 路径重写
          rewrite: (path) => path.replace(/^\/api/, ''),
          // rewrite: (path) => path.replace('^/api', ''),
        }
      }
    }
  }
})
